import sys
import json


sys.path.append(r'./')

from codesecurity.tasks.code_obfuscation_detect.cod import *
from codesecurity.data.api import *

import numpy as np
import time

'/mnt/XiaoweiGuo/data/npm_packages/hot_package/preprocess'
'/home/passwd123/XiaoweiGuo/vscode/code-security/data/js/jstap_train/good'
'/home/passwd123/XiaoweiGuo/vscode/code-security/data/js/jstap_train/bad'
good_dir='/mnt/XiaoweiGuo/data/npm_packages/hot_package/preprocess'
bad_dir='/home/passwd123/XiaoweiGuo/vscode/code-security/data/js/jstap_train/good'

def normal_distri(normal_result:list):
    sorted(normal_result)
    
    std=np.std(normal_result)
    
    
    min_value=normal_result[0]
    max_value=normal_result[-1]
    
    return min_value-std*2.,max_value+std*2.

start_time=time.time()

files=iter_dir(good_dir)
files=[e for e in files if e.endswith('.js')]

print('good files: ',len(files))
good_e=[compute_entropy(e) for e in files]

min_value,max_value=normal_distri(good_e)#1.44917621985402,1.8714918484595873#normal_distri(good_e) #1.44917621985402 1.8714918484595873


files=iter_dir(bad_dir)
files=[e for e in files if e.endswith('.js')]

bad_e=[compute_entropy(e) for e in files]


obs_max=[]
obs_min=[]

for i in range(len(bad_e)):
    if bad_e[i]>=max_value:
      print(files[i])
      obs_max.append(files[i])  

print(f'find {len(obs_max)} obs file in max.')

for i in range(len(bad_e)):
    if bad_e[i]<min_value:
      print(files[i])
      obs_min.append(files[i])  

print(f'find {len(obs_min)} obs file in min.')


end_time=time.time()

print(f'spend {end_time-start_time} seconds.')

print(min_value,max_value)
# print('bad files: ',len(files))
# bad_e=[compute_entropy(e) for e in files]
# print(f'bad: {np.mean(bad_e)} {np.std(bad_e)}')
